package guoanmaker.operator.business.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.model.OperatorCities;
import guoanmaker.operator.business.model.OperatorPatent;
import guoanmaker.operator.business.model.ProvideCommodity;
import guoanmaker.operator.business.model.ProvideCommodityServiceRange;
import guoanmaker.operator.business.model.ProvideRoom;
import guoanmaker.operator.business.modelnobind.OperatorPatentCommodityVO;
import guoanmaker.operator.business.repository.OperatorPatentRepository;
import guoanmaker.operator.business.repository.ProvideCommodityRepository;

@Service
public class OperatorPatentService implements OperatorPatentServiceInterface {
	@PersistenceContext
	private EntityManager manager;
	@Autowired
	private OperatorPatentRepository operatorPatentRepository;
	@Autowired
	private ProvideCommodityRepository provideCommodityRepository;
	@Autowired
	private OperatorCitiesServiceInterface operatorCitiesServiceInterface;
	@Autowired
	private OperatorRoomServiceInterface operatorRoomServiceInterface;
	@Autowired
	private OperatorBoutiqueRecommendServiceInterface operatorBoutiqueRecommendServiceInterface;
	@Override
	@Transactional
	public OperatorPatent saveOperatorPatent(OperatorPatent operatorPatent) {
	/*	int a=0;
		
		  String ss="";
		  try{
			  if (!"".equals(operatorPatent.getCommodityNum())) {
				  if(operatorPatent.getCommodityNum().contains("S")){
					  ProvideRoom provideRoom=operatorRoomServiceInterface.selectProvideRoomByroomCodes(operatorPatent.getCommodityNum());
					  if(provideRoom==null){
						  a=1;//商品不存在,请查证后再输/
						  ss="商品不存在,请查证后再输";
					  }else{
					  OperatorCities operatorCities= operatorCitiesServiceInterface.getCitiesByCode(operatorPatent.getCitycode());
					  ProvideRoom pr= operatorRoomServiceInterface.selectProvideRoomByroomCodesAndcityId(operatorPatent.getCommodityNum(), operatorCities.getId());
					  if(pr==null){
						  a=2;//该商品不在该服务区域
						  ss="该商品不在该服务区域";
					  }
					  }
				  }else{
					  Integer code=Integer.parseInt(operatorPatent.getCommodityNum());
					  ProvideCommodity provideCommodity=operatorBoutiqueRecommendServiceInterface.selectProvideCommodity(code);
					  if(provideCommodity==null){
						  a=1;//商品不存在或是空间产品
						  ss="商品不存在或是空间产品";
					  }else{
						  if(!provideCommodity.getIsTerritoryRestriction().equals("2")){//2是全国
							  OperatorCities operatorCities= operatorCitiesServiceInterface.getCitiesByCode(operatorPatent.getCitycode());
							  List<ProvideCommodityServiceRange> serrangs= operatorBoutiqueRecommendServiceInterface.selectProvideCommodityServiceRange(provideCommodity.getId(), operatorCities.getId());
							  if(serrangs.size()==0){
								  a=2;//该商品不在该服务区域
								  ss="该商品不在该服务区域";
							  }
						  }
					  }
				  }
				 return  operatorPatentRepository.save(operatorPatent);
				 // ajaxJson.setSuccess(true).setStatus(200).setData(a).setMsg(ss);
			  }else {
				  operatorPatentRepository.save(operatorPatent);  
			  }
			  
		 
		 
	    	}catch(Exception e){
	    		a=3;
	    		ss="系统错误";
	    		//ajaxJson.setSuccess(false).setStatus(250).setMsg(ss).setData(a);
	    		
	    	}
		*/
		
		
		
		return operatorPatentRepository.save(operatorPatent);
	}

	@Transactional
	public void delectAllBycitycode(String citycode, int type) {
		String jpql = "delete  from OperatorPatent  u where u.citycode=:citycode and type=:type";
		Query query2 = manager.createQuery(jpql);// 查询商品提供的服务区域
		query2.setParameter("citycode", citycode);
		query2.setParameter("type", type);
		query2.executeUpdate();

	}

	@Override
	public void deleteOperatorPatent(String id) {
		// TODO Auto-generated method stub
		operatorPatentRepository.delete(id);
	}

	@Override
	public List<OperatorPatent> selectOperatorPatent(String citycode, int type) {
		// TODO Auto-generated method stub
		return operatorPatentRepository.selectByOperatorPatent(type, citycode);
	}

	@Override
	public OperatorPatentCommodityVO selectOperatorPatentByCommodityNum(Integer commodityNum, String cityCode) {
		OperatorPatentCommodityVO commodityVO = null;
		ProvideCommodity commodity = provideCommodityRepository.selectByCommodityBypraThree(commodityNum);
		if (commodity != null) {
			
			if (commodity.getIsTerritoryRestriction().equals("1")) {
				List<ProvideCommodityServiceRange> commodityServiceRanges = commodity.getServiceRange();
				if (commodityServiceRanges != null && commodityServiceRanges.size() >0) {
					for (ProvideCommodityServiceRange provideCommodityServiceRange : commodityServiceRanges) {
						if (cityCode.equals(provideCommodityServiceRange.getCity().getCode())) {
							commodityVO = new OperatorPatentCommodityVO();
							commodityVO.setCommidityId(commodity.getId());
							commodityVO.setCommodityName(commodity.getCommodityName());
							commodityVO.setSmall_pic(commodity.getCommodity_smallimage());
							commodityVO.setFinalPrice(String.valueOf(provideCommodityServiceRange.getFinalPrice()));
							break;
							
						} 
					}
				}
				
			} else {
				commodityVO = new OperatorPatentCommodityVO();
				commodityVO.setCommidityId(commodity.getId());
				commodityVO.setCommodityName(commodity.getCommodityName());
				commodityVO.setSmall_pic(commodity.getCommodity_smallimage());
				commodityVO.setFinalPrice(String.valueOf(commodity.getFinalPrice()));
			}
		}
		return commodityVO;
	}

	@Override
	public List<OperatorPatentCommodityVO> getAlltOperatorPatentCommodity(String cityCode,int type) {
		List<OperatorPatent> operatorPatents = operatorPatentRepository.selectByOperatorPatent(type,cityCode);
		List<OperatorPatentCommodityVO> commodityVOs = new ArrayList<>();
		OperatorPatentCommodityVO commodityVO = null;
		for (OperatorPatent operatorPatent : operatorPatents) {
			if (operatorPatent.getCommodityNum() != null && !"".equals(operatorPatent.getCommodityNum())) {
			ProvideCommodity commodity = provideCommodityRepository.selectByCommodityBypraThree(Integer.parseInt(operatorPatent.getCommodityNum()));
			if (commodity != null) {
				if (commodity.getIsTerritoryRestriction().equals("1")) {
					List<ProvideCommodityServiceRange> commodityServiceRanges = commodity.getServiceRange();
					if (commodityServiceRanges != null && commodityServiceRanges.size() >0) {
						for (ProvideCommodityServiceRange provideCommodityServiceRange : commodityServiceRanges) {
							if (cityCode.equals(provideCommodityServiceRange.getCity().getCode())) {
								commodityVO = new OperatorPatentCommodityVO();
								commodityVO.setCommidityId(commodity.getId());
								commodityVO.setCommodityName(commodity.getCommodityName());
								/*if (type == 1) {
									commodityVO.setSmall_pic(operatorPatent.getComPrcUrl());
								} else {
									commodityVO.setSmall_pic(commodity.getCommodity_smallimage());
								}*/
								commodityVO.setSmall_pic(commodity.getCommodity_smallimage());
								commodityVO.setServiceType(commodity.getType());
								commodityVO.setFinalPrice(String.valueOf(provideCommodityServiceRange.getFinalPrice()));
								break;
								
							}
						}
					}
					
				} else {
					commodityVO = new OperatorPatentCommodityVO();
					commodityVO.setCommidityId(commodity.getId());
					commodityVO.setCommodityName(commodity.getCommodityName());
					/*if (type == 1) {
						commodityVO.setSmall_pic(operatorPatent.getComPrcUrl());
					} else {
						commodityVO.setSmall_pic(commodity.getCommodity_smallimage());
					}*/
					commodityVO.setSmall_pic(commodity.getCommodity_smallimage());
					commodityVO.setServiceType(commodity.getType());
					commodityVO.setFinalPrice(String.valueOf(commodity.getFinalPrice()));
				}
				commodityVOs.add(commodityVO);
			}
		}	
		}
		return commodityVOs;
	}

	
}
